import requests
from lxml import etree


def post_C2(request_url, csp, lsp, CorrelateID, CmdFile):
    # request_url = "http://10.48.115.55:6080/axis/services/ctms"
    headers = {'SOAPAction': '',
               'User-Agent': 'gSOAP/2.7',
               'Content-Type': 'text/xml; charset=utf-8',
               'Connection': 'close',
               }
    requestxmldata = '<?xml version="1.0" encoding="UTF-8"?>' \
                     '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:impl="iptv">' \
                     '<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' \
                     '<impl:ExecCmd>' \
                     '<CSPID xsi:type="xsd:string">' + csp + '</CSPID>' \
                     '<LSPID xsi:type="xsd:string">' + lsp + '</LSPID>' \
                     '<CorrelateID xsi:type="xsd:string">' + CorrelateID + ' </CorrelateID>' \
                     '<CmdFileURL xsi:type="xsd:string">' +CmdFile + '</CmdFileURL>' \
                      '</impl:ExecCmd>' \
                      '</SOAP-ENV:Body>' \
                      '</SOAP-ENV:Envelope>'

    print("start send C2 SOAP request")
    response = requests.post(url=request_url, headers=headers, data=requestxmldata,verify=False)
    """
    tree = etree.fromstring(response.text.encode('utf-8')
    respresult = tree.xpath("//ExecCmdReturn/Result")[0].text
    if respresult == "0":
        print("send C2 SOAP request successfully!")
        print("=============================================")          
    else:
        print("send C2 SOAP request failed,please check the under and upper node configuration!!")

    """

